<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="mini" @change="datehange">
        <el-option
          v-for="year in yearList"
          :key="year"
          :label="year+'年'"
          :value="year"
        />
      </el-select>
      <el-select v-model="currentMonth" size="mini" @change="datehange">
        <el-option
          v-for="month in 12"
          :key="month"
          :label="month+'月'"
          :value="month"
        />
      </el-select>
    </el-row>

    <!-- 日历组件 -->
    <el-calendar v-model="currentDate">
      <!-- 使用插槽自定义显示日期和周末休息 -->
      <template #dateCell="{date,data}">
        <div class="date-content">
          <span class="text"> {{ data.day | getDay }}</span>
          <span v-if="isWeekend(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  filters: {
    getDay(oldVal) {
      const str = oldVal.split('-')[2]
      // substring(从第几个字符串开始,从第几个字符串截止)  提取字符串中的数据
      return str.startsWith('0') ? str.substring(1) : str
    }
  },
  data() {
    const currentDate = new Date()
    const currentYear = currentDate.getFullYear()
    const currentMonth = currentDate.getMonth() + 1
    const yearList = []
    for (let i = 0; i < 11; i++) {
      // 当前时间减去前5年在加上 11 获取到现在时间的前后5年时间
      yearList.push(currentYear - 5 + i)
    }
    return {
      currentDate,
      currentYear,
      currentMonth,
      yearList
    }
  },
  watch: {
    //  监听日历数据变化  让下拉菜单同步更新
    currentDate() {
      this.currentYear = this.currentDate.getFullYear()
      this.currentMonth = this.currentDate.getMonth() + 1
    }
  },
  methods: {
    datehange() {
      // 日期菜单数据发生改变时触发
      this.currentDate = new Date(`${this.currentYear - this.currentMonth}`)
    },
    isWeekend(date) {
      // getDay() 获取指定日期是星期几 星期日为 0，
      return date.getDay() === 6 || date.getDay() === 0
    }
  }
}
</script>

<style  scoped>
 ::v-deep .el-calendar-day {
  height:  auto;
 }
 ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
</style>
